package com.muyu.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.system.domain.DanmoShoppingProducts;
import com.muyu.system.domain.DanmoShoppingReviews;
import com.muyu.system.req.DanmoShoppingProductsPriceReq;
import com.muyu.system.service.IDanmoShoppingProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 商品Controller
 * @author yangle
 * @date 2024-09-29
 */
@RestController
@RequestMapping("/products")
public class DanmoShoppingProductsController extends BaseController {
    @Autowired
    private IDanmoShoppingProductsService danmoShoppingProductsService;

    /**
     * 查询商品列表
     */
//    @RequiresPermissions("system:products:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoShoppingProducts danmoShoppingProducts) {
        PageHelper.startPage(danmoShoppingProducts.getPageNum(), danmoShoppingProducts.getPageSize());
        List<DanmoShoppingProducts> list = danmoShoppingProductsService.selectDanmoShoppingProductsList(danmoShoppingProducts);
        PageInfo<DanmoShoppingProducts> danmoShoppingReviewsPageInfo = new PageInfo<>(list);
        return Result.success(danmoShoppingReviewsPageInfo);
    }

    /**
     * 导出商品列表
     */
//    @RequiresPermissions("system:products:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoShoppingProducts danmoShoppingProducts) {
        List<DanmoShoppingProducts> list = danmoShoppingProductsService.selectDanmoShoppingProductsList(danmoShoppingProducts);
        ExcelUtil<DanmoShoppingProducts> util = new ExcelUtil<DanmoShoppingProducts>(DanmoShoppingProducts.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
//    @RequiresPermissions("system:products:query")
    @GetMapping(value = "/details/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.warn("请登录");
        }
        DanmoShoppingProducts danmoShoppingProducts = danmoShoppingProductsService.selectDanmoShoppingProductsById(id);
        return Result.success(danmoShoppingProducts);
    }

    /**
     * 新增商品
     */
//    @RequiresPermissions("system:products:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody DanmoShoppingProducts danmoShoppingProducts) {
        return danmoShoppingProductsService.insertDanmoShoppingProducts(danmoShoppingProducts);
    }

    /**
     * 修改商品
     */
//    @RequiresPermissions("system:products:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoShoppingProducts danmoShoppingProducts) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.warn("请登录");
        }
        return danmoShoppingProductsService.updateDanmoShoppingProducts(danmoShoppingProducts);
    }

    /**
     * 删除商品
     */
//    @RequiresPermissions("system:products:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return toAjax(danmoShoppingProductsService.deleteDanmoShoppingProductsByIds(ids));
    }

    /**
     * 商品价格排序
     */
    @PostMapping("priceSort")
    public Result selectPriceSort(@RequestBody DanmoShoppingProductsPriceReq danmoShoppingProductsPriceReq) {
        List<DanmoShoppingProducts> list = danmoShoppingProductsService.selectPriceSort(danmoShoppingProductsPriceReq);
        return Result.success(list);
    }

    /**
     * 商品销量排序
     *
     * @param danmoShoppingProductsPriceReq
     * @return
     */
    @PostMapping("salesSortList")
    public Result salesSortList(@RequestBody DanmoShoppingProductsPriceReq danmoShoppingProductsPriceReq) {
        List<DanmoShoppingProducts> list = danmoShoppingProductsService.salesSortList(danmoShoppingProductsPriceReq);
        return Result.success(list);
    }

    /**
     * 评分排序
     */
    @PostMapping("commentsNumbersSort")
    public Result commentsNumbersSort(@RequestBody DanmoShoppingProductsPriceReq danmoShoppingProductsCollection) {
        List<DanmoShoppingProducts> list = danmoShoppingProductsService.commentsNumbersSort(danmoShoppingProductsCollection);
        return Result.success(list);
    }

    /**
     * 排行榜
     */
    @PostMapping("ranking")
    public Result ranking(@RequestBody DanmoShoppingProductsPriceReq danmoShoppingProductsPriceReq) {
        List<DanmoShoppingProductsPriceReq> list = danmoShoppingProductsService.commentsNumbersSortTop(danmoShoppingProductsPriceReq);
        return null;
    }




}
